#include <gtk/gtk.h>
#include <cairo/cairo.h>
#include "common.h"

static gint count = 0;
static char buf[5];

#define H_NUM    100
#define W_NUM    100

static gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
    GtkWidget *window = GTK_WIDGET(data);
    cairo_t *cr = NULL;
    gint w,h;
    gfloat interW, interH;
    gdouble x = 0.0;
    gdouble y = 0.0;
    gdouble red = 0.0;
    gdouble blue = 0.0;
    gdouble green = 0.0;
    gdouble interColor;

    gtk_widget_get_size_request(window, &w, &h);

    interH = h / H_NUM;
    interW = w / W_NUM;

    interColor = 1.0 / (H_NUM * W_NUM) * 3;

    /* cr */
    cr = gdk_cairo_create(widget->window);

    for (y = 0; y < h; y += interH) {
        for (x = 0; x < w; x += interW) {
            g_message("%f:%f:%f", red, green , blue);
            cairo_set_source_rgb(cr, red, green, blue);
            cairo_move_to(cr, x, y);
            cairo_rectangle(cr, x, y, interW, interH);
            cairo_fill(cr);

            blue += interColor;
            green += interColor;
            red += interColor;
        }
    }
    cairo_destroy(cr);
    return FALSE;
}


int main(int argc,char *argv[])
{
    GtkWidget *window;
    GtkWidget *drawingArea = NULL;

	gtk_init(&argc,&argv);

	/* create the main, top level, window */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), TITLE);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
//	gtk_window_set_default_size(GTK_WINDOW(window), 250, 180);
    gtk_widget_set_size_request(window, 300, 250);
//	gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color_black_); /*设置为黑色背景*/
//    gtk_window_fullscreen(GTK_WINDOW(window));

    /*drawingArea*/
    drawingArea = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(window), drawingArea);
    g_signal_connect(GTK_OBJECT(drawingArea), "expose-event", G_CALLBACK(on_expose_event), window);
//    gtk_widget_queue_draw(window);


    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
	/* make sure that everything, window and label, are visible */
	gtk_widget_show_all(window);

	/* start the main loop, and let it rest until the application is closed */
	gtk_main();

	return 0;
} 
